<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title></title>
	</head>

	<body>
	</body>
	<script type="text/javascript">
		/*封装ForEach方法*/
		/** 
		 * forEach遍历数组 
		 * @param callback [function] 回调函数； 
		 * @param context [object] 上下文； 
		 */
		/*Array.prototype.myForEach = function myForEach(callback, context) {
			context = context || window;
			if('forEach' in Array.prototye) {
				this.forEach(callback, context);
				return;
			}
			//IE6-8下自己编写回调函数执行的逻辑  
			for(var i = 0, len = this.length; i < len; i++) {
				callback && callback.call(context, this[i], i, this);
			}
		}*/
		

		/*封装Map方法*/
		/** 
		 * map遍历数组 
		 * @param callback [function] 回调函数； 
		 * @param context [object] 上下文； 
		 */
		/*Array.prototype.myMap = function myMap(callback, context) {
			console.log(callback);
			console.log(context);
			context = context || window;
			if('map' in Array.prototye) {
				return this.map(callback, context);
			}
			//IE6-8下自己编写回调函数执行的逻辑  
			var newAry = [];
			for(var i = 0, len = this.length; i < len; i++) {
				if(typeof callback === 'function') {
					var val = callback.call(context, this[i], i, this);
					newAry[newAry.length] = val;
				}
			}
			return newAry;
		}
		var arr = [1, 2, 3];
		arr.myMap(1, arr);*/

		/*Array.prototype.remove = function(item) {
			var idx = this.indexOf(item);
			if(idx >= 0) {
				return this.splice(idx, 1);
			}
			return null;
		}
		var arr = [1, 3, 5, 2, 4];
		console.log(arr.remove(3), arr);
		for(var i in arr) {
			if(typeof arr[i] === 'number') {
				console.log(arr[i]);
			} else {
				console.log([i, '什么鬼']);
			}
		}*/

		Array.prototype.each = function(fun) {
			//无论是在类的构造函数还是在原型中，this都指向实例化的对象，这里指的是调用each方法的数组对象.   
			for(var i = 0, n = this.length; i < n; i++) {
				fun(i, this[i]);
			}
		}
		Array.prototype.map = function(fun) {
			var o = [];
			this.each(function(k, v) {
				o[k] = fun(k, v);
			});
			return o;
		}
		var arr = [1, 2, 3, 4];
		var str = "";
		arr.each(function(k, v) {
			str += k + ":" + v + "\n";
		});
		r = arr.map(function(k, v) {
			return v * 2;
		});
//		console.log(str);
//		console.log(r);
		
		Array.prototype.map = function (fun) {
			var _this = this;
			for (var i = 0; i < _this.length; i++) {
				_this[i] = fun.call(this, _this[i]);
			}
			return _this;
		}
		var arr = [1, 2, 3];
		 r = arr.map(function(x) {
			x += 1;
			return x;
		})
		 console.log(r);
	</script>

</html>